package com.gandong8.hbase.web.interceptor;


import com.gandong8.hbase.web.exception.LoginException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
public class AuthInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        if(!(o instanceof HandlerMethod)){
            return true;
        }
        String path = httpServletRequest.getServletPath();
        if("/login".equals(path) || "/login/out".equals(path)
                || "/login/do".equals(path)
                || "/404".equals(path) || "/err".equals(path)) {
            return true;
        }
        if (httpServletRequest.getSession().getAttribute("login_user") == null) {
            throw new LoginException("please login first");
        }
        return true;
    }

}
